package com.hznc.wcs.config;



import com.intelligt.modbus.jlibmodbus.Modbus;
import com.intelligt.modbus.jlibmodbus.master.ModbusMaster;
import com.intelligt.modbus.jlibmodbus.master.ModbusMasterFactory;
import com.intelligt.modbus.jlibmodbus.tcp.TcpParameters;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;

@Configuration
public class ModbusConfig {
    @Value("${modbus.host}")
    private String host;
    @Value("${modbus.port}")
    private int port;
    //读线程的标识,在业务需要时可以控制程序的状态
    public static boolean FLAG = true;
    //读间隔时间
    public static final long READ_SPACE = 10000;
    private ModbusMaster master;

    @PostConstruct
    public void connect() {
        new Thread(new Runnable() {
            public void run() {
                try {
                    //设置主机TCP参数
                    TcpParameters tcpParameters = new TcpParameters();
                    //设置TCP的IP地址
                    InetAddress inetAddress = InetAddress.getByName(host);
                    //TCP参数设置IP地址
                    tcpParameters.setHost(inetAddress);
                    //TCP设置长连接
                    tcpParameters.setKeepAlive(true);
                    //tcp设置端口,这里使用Modbus的默认端口502
                    tcpParameters.setPort(port);
                    //创建一个主机
                    ModbusMaster modbusMaster = ModbusMasterFactory.createModbusMasterTCP(tcpParameters);
                    //Modbus.setLogLevel(Modbus.LogLevel.LEVEL_DEBUG);
                    //设置自增的id
                    Modbus.setAutoIncrementTransactionId(true);

                    if (!modbusMaster.isConnected()) {

                        System.out.println("Modbus主机没有连接,现在建立连接");
                        modbusMaster.connect();
                    } else {
                        System.out.println("Modbus主机已经连接");
                    }

                    while (FLAG) {
                        //寄存器读取开始地址
                        int offset = 0;
                        //读取寄存器数量
                        int quantity = 10;
                        //读取对应从机的数据
                        //注意这里使用的read函数,要根据寄存器的方法属性选用不同的读函数
                        //readHoldingRegisters
                        //readInputRegisters
                        //readCoils
                        //readDiscreteInputs
                        int[] registers = modbusMaster.readHoldingRegisters(1, offset, quantity);
//                        boolean[] readCoils = modbusMaster.readCoils(1, offset, quantity);
                        for (int register : registers) {
                            System.out.println("读取寄存器数据:" + offset++ + "-" + register);
                            //修改寄存器的值
                            modbusMaster.writeSingleRegister(1, 9,  4);
                        }
                        System.out.println("--------------------------------------------");
                        Thread.sleep(READ_SPACE);
                    }
                } catch (Exception e) {
                    System.out.println("jlibmodbus运行异常:" + e.getClass().getName() + "-->" + e.getMessage());
                    e.printStackTrace();
                }
            }
        }).start();

    }

}
